package com.decent.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.decent.entity.Order;
import com.decent.vo.OrderDetailVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author decent
 */
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    /**
     *插入订单
     * @param record
     * @return
     */
    @Override
    @Insert("insert into `order` (    " +
            "   user_order_no," +
            "   product_id," +
            "   `count`, " +
            "   pay_price, " +
            "   total_price, " +
            "   integral, " +
            "   order_status, " +
            "   buy_time, " +
            "   create_time, " +
            "   uid)" +
            "values ( " +
            "   #{userOrderNo}," +
            "   #{productId}," +
            "   #{count,jdbcType=INTEGER}, " +
            "   #{payPrice,jdbcType=DECIMAL}," +
            "   #{totalPrice,jdbcType=VARCHAR}, " +
            "   #{integral}, " +
            "   #{orderStatus,jdbcType=INTEGER}, " +
            "   #{buyTime,jdbcType=TIMESTAMP}," +
            "   #{createTime}, #{uid}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(Order record);

    /**
     * 选择用户订单
     * @param orderStatus
     * @param uid
     * @return
     */
    @Select("    SELECT\n" +
            "      o.id,\n" +
            "      p.product_name,\n" +
            "      brand.brand_name,\n" +
            "      o.user_order_no,\n" +
            "      o.buy_time,\n" +
            "      p.buy_after_effective_time,\n" +
            "      p.original_price,\n" +
            "      p.pic_url AS iconUrl,\n" +
            "      o.pay_price,\n" +
            "      o.order_status\n" +
            "    FROM\n" +
            "      brand\n" +
            "        JOIN product AS p ON brand.id = p.brand_id\n" +
            "        JOIN `order` AS o ON p.id = o.product_id\n" +
            "    WHERE\n" +
            "      o.uid = #{uid}\n" +
            "      AND o.order_status = #{orderStatus}")
    List<OrderDetailVO> selectUserOrder(@Param("orderStatus") String orderStatus,
                                        @Param("uid") Integer uid);

    /**
     * 订单详情
     * @param orderId
     * @param uid
     * @return
     */
    @Select("    SELECT\n" +
            "      p.product_name,\n" +
            "      brand.brand_name,\n" +
            "      p.buy_after_effective_time,\n" +
            "      brand.icon_url,\n" +
            "      card.card_number,\n" +
            "      card.card_pwd,\n" +
            "      p.use_notice\n" +
            "    FROM\n" +
            "      product AS p\n" +
            "        JOIN `order` AS o ON p.id = o.product_id\n" +
            "        JOIN brand ON brand.id = p.brand_id\n" +
            "        JOIN card ON card.product_id = p.id AND card.order_id = o.id\n" +
            "    WHERE\n" +
            "      o.uid = #{uid} AND o.id = #{orderId}")
    List<OrderDetailVO> detailsOrder(@Param("orderId") Long orderId,
                                     @Param("uid") Integer uid);

    /**
     * 插入多个订单
     * @param orders
     * @return
     */
    @Insert("<script>" +
            "    INSERT INTO `order` (    " +
            "       user_order_no," +
            "       product_id,`count`, " +
            "       pay_price, " +
            "       total_price, " +
            "       integral, " +
            "       order_status, " +
            "       buy_time, " +
            "       create_time, " +
            "       uid)" +
            "    VALUES\n" +
            "    <foreach collection=\"list\" item=\"order\" separator=\",\">\n" +
            "      (#{order.userOrderNo}," +
            "       #{order.productId}," +
            "       #{order.count}," +
            "       #{order.payPrice}," +
            "       #{order.totalPrice}," +
            "       #{order.orderStatus}," +
            "       #{order.buyTime}," +
            "       #{order.createTime}," +
            "       #{order.uid})\n" +
            "    </foreach>" +
            "</script>")
    int insertOrders(List<Order> orders);
}